<template>
	<view class="bg-light" style="height: 100%;">
		<scroll-view scroll-y="true" class="w-100" style="height:calc(100vh - 110rpx);">
			<view class="py-2">
				<view class="mx-2 bg-white rounded-lg text-dark">
					<!-- 地址选择 -->
					<view  @click="handleAddress()" class="ml-2 py-2 flex justify-between align-center">
						<!-- 已选中地址 -->
						<view v-if="customerAddress" class="flex flex-column">
							<text class="font">{{customerAddress.Province}}{{customerAddress.City}}{{customerAddress.County}}</text>
							<text class="font-md my-2 font-weight-bold">{{customerAddress.Details}}</text>
							<text class="font">{{customerAddress.Name}} {{customerAddress.Phone}}</text>
						</view>
						<!-- 未选中地址 -->
						<view v-else class="flex flex-column">
							<text class="font">暂未获取到收货地址</text>
							<text class="font-lg my-2 font-weight-bold">请选择收货地址</text>
							<text class="font"></text>
						</view>
						<uni-icons type="right" class="mr-2" color="#828d96" size="16"></uni-icons>
					</view>
				</view>
			</view>
			<!-- 商品信息 -->
			<view class="mx-2 bg-white py-2 rounded-lg">
				<view class="mx-2 mt-2" v-for="obj in Data" :key="obj.Activity_SerialNumber">
					<!-- 所属活动 -->
					<view class="flex justify-between align-center" v-if="obj.Activity_SerialNumber">
						<view class="flex">
							<text class="flex-shrink font font-weight-bold">活动:</text><text class="font text-ellipsis">{{obj.Activity_Name}}</text>
							<uni-icons class="flex-shrink" color="#bbb" size="16" type="right"></uni-icons>
						</view>
						<text class="font-sm text-danger flex-shrink">已用积分:({{obj.Sales_Point}}/{{obj.Remain_Points}})</text>
					</view>
					<!-- 商品 -->
					<view v-for="(item,index) in obj.Children" :key="index" class="mt-2">
						<view class="flex">
							<view class="flex-shrink rounded overflow-hidden shadow" style="width: 180rpx; height: 180rpx;">
								<image :src="BASE_IMG+item.Specifications_Picture_URL" style="width: 100%;height: 100%;" mode=""></image>
							</view>
							<view class="ml-2 flex flex-column justify-between w-100">
								<text class="font font-weight-bold">{{item.Article_Name}}</text>
								<text class="font-sm text-muted">{{item.Specifications_Name}}</text>
								<text v-if="Point_Config.Row_Status=='0'&&obj.Activity_SerialNumber" class="font-sm" :class="item.Cash_Point=='0'?'text-success':'text-muted'">
									{{item.Cash_Point=='0'?'单商品可以积分'+ Points_Prompt(item.Deduction_Type,item.Deduction_Limit):'暂无法使用积分'}}
								</text>
								<view class="flex justify-between">
									<money :money="item.Sales_Unit_Price"></money>
									<counter v-model="item.Quantity" @change="handleQuantity($event,item)"></counter>
								</view>
							</view>
						</view>
						<!-- 实付款 -->
						<uni-collapse v-if="obj.Activity_SerialNumber">
							<uni-collapse-item title="实付款" :showArrow="false">
								<template v-slot:title>
									<view class="flex justify-between align-center py-1">
										<view>
											<text class="font-sm font-weight-bold">实付款</text>
											<uni-icons color="#bbb" size="14" type="bottom"></uni-icons>
										</view>
										<money color="#000" :money="item.Reality_Amount"></money>
									</view>
								</template>
								<view class="flex justify-between align-center py-1">
									<text class="font-sm font-weight-bold">商品总价</text>
									<money color="#000" :money="item.Amount"></money>
								</view>
								<view class="flex justify-between align-center py-1">
									<text class="font-sm font-weight-bold">积分抵扣</text>
									<money color="#dc3545" before="减" :money="item.Deduction"></money>
								</view>
								<view class="flex justify-between align-center py-1">
									<text class="font-sm font-weight-bold">积分消费</text>
									<text class="font-sm font-weight-bold">{{item.Sales_Point}}</text>
								</view>
							</uni-collapse-item>
						</uni-collapse>
						<view v-else class="flex justify-between align-center py-1">
							<text class="font-sm font-weight-bold">应付款</text>
							<money color="#000" :money="item.Reality_Amount"></money>
						</view>
					</view>
					<view @click="handlePoints(obj.Activity_SerialNumber)" class="flex justify-between align-center mt-2" v-if="obj.Activity_SerialNumber">
						<view class="flex align-center">
							<text class="font" :class="PointsColor(obj.Activity_SerialNumber)?'text-dark':'text-danger'">{{member_zl[obj.Activity_SerialNumber]?member_zl[obj.Activity_SerialNumber].filter(i=>i.isSelect)[0].Department_Name:''}}</text>
							<view v-if="member_zl[obj.Activity_SerialNumber]&&member_zl[obj.Activity_SerialNumber].length > 1" class="font flex align-center ml-1 text-danger">
								<text class="font-small">点击更换</text>
								<uni-icons color="#dc3545" size="12" type="right"></uni-icons>
							</view>
						</view>
						<view class="flex align-center flex-shrink">
							<uni-icons fontFamily="iconfont" color="#ffc107" size="20">{{'\ue61a'}}</uni-icons>
							<text class="font text-warning">{{member_zl[obj.Activity_SerialNumber]?member_zl[obj.Activity_SerialNumber].filter(i=>i.isSelect)[0].Remain_Points:0}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 总计 -->
			<view class="m-2 py-2 bg-white rounded-lg">
				<view class="mx-2 flex flex-column">
					<!-- 商品总价 -->
					<view class="flex mb-2 justify-between">
						<view><!-- 左 -->
							<text class="font text-dark">商品总价</text>
							<text class="ml-1 font text-muted">共{{Total.Quantity}}件商品</text>
						</view>
						<view><!-- 右 -->
							<text class="font text-dark font-weight-bold">￥{{Total.Reality_Amount}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 支付方式 -->
			<view v-for="(item,index) in pay" :key="index" @click="handlePay(item)"
				class="mx-2 py-2 bg-white rounded-lg flex justify-between">
				<view class="ml-2 flex align-center">
					<uni-icons fontFamily="iconfont" :color="item.Color" size="20">{{item.Icon}}</uni-icons>
					<text class="font ml-2 text-dark">{{item.Name}}</text>
				</view>
				<view class="mr-2">
					<uni-icons v-if="!item.isSelect" type="circle" :color="item.Color" size="20"></uni-icons>
					<uni-icons v-else type="checkbox-filled" :color="item.Color" size="20"></uni-icons>
				</view>
			</view>
			<!-- 买家留言 -->
			<view class="m-2 py-2 bg-white flex rounded-lg">
				<text class="ml-2 font flex-shrink pr-4">买家留言</text>
				<textarea v-model="Sales_Message" class="font text-right w-100 mr-2"
					placeholder-class="text-muted text-right" placeholder="留言建议提前协商 (250字以内) " auto-height></textarea>
			</view>
		</scroll-view>
		<!-- 底部提交 -->
		<view class="fixed-bottom shadow flex align-center justify-between bg-white" style="height: 110rpx;">
			<view class="ml-3 flex align-end">
				<text class="font-lg">应付：</text>
				<text class="font-sm text-success">￥</text>
				<text class="font-lg text-success">{{Total.Reality_Amount}}</text>
			</view>
			<view @click="submit" class="mr-3 px-3 py-2 rounded-circle flex align-center"
				:style="{backgroundColor:pay.filter(i=>i.isSelect)[0].Color}">
				<text class="text-white font">{{pay.filter(i=>i.isSelect)[0].Name}}</text>
			</view>
		</view>
		<popUp :show="showMask" @Close="()=>{this.showMask=!this.showMask}" title="更换部门" Height="60">
			<view class="py-2">
				<view @click="handlePoints(item.Department_SerialNumber,true)" v-for="(item,index) in member.filter(i=>i.Activity_SerialNumber == Activity_SerialNumber)" :key="index" class="shadow mx-2 mb-2 bg-white rounded-lg py-3 flex align-center">
					<view class="flex justify-between align-center w-100">
						<view class="flex align-center ml-2 w-100">
							<text class="font font-weight-bold flex-shrink">部门名称：</text>
							<text class="font text-dark text-ellipsis-2">{{item.Department_Name}}</text>
						</view>
						<view class="flex align-center flex-shrink mr-2">
							<uni-icons fontFamily="iconfont" color="#ffc107" size="20">{{'\ue61a'}}</uni-icons>
							<text class="font text-warning">{{item.Remain_Points}}</text>
						</view>
					</view>
					<view class="mr-2 flex-shrink">
						<uni-icons :type="item.isSelect?'checkbox-filled':'circle'" color="#ffc107" size="20"></uni-icons>
					</view>
				</view>
			</view>
		</popUp>
	</view>
</template>

<script>
	const app = getApp();
	import { mapState } from 'vuex';
	import money from "@/components/tool/money.vue";
	import counter from "@/components/tool/counter.vue";
	import popUp from "@/components/tool/pop-up.vue";
	import Base64 from "base-64";
	import {accAdd,accMul,accDiv,accSub,addZero,weChatPay} from "@/static/js/GeneralUtil.js";
	import {salesOrderJSON , salesOrderDdetailJSON , collectionDetailJSON} from "@/static/js/OrderUtil.js";
	import {DateTime,getCurrentTimestamp} from "@/static/js/DateUtil.js";
	import WebSocketIO from "@/static/js/WebSocket.js"
	import {generalSave , generalQueryByTableName ,generalQueryBySQLSerialNumber ,getMemberPoint} from "@/api/generalBusiness.js";
	import {saveSalesOrder,saveReceipts,rewriteReceipts} from "@/api/salerOrder.js";
	export default {
		components: {
			money,
			counter,
			popUp
		},
		data() {
			return {
				customerAddress: null, //已选地址
				Article: [], //商品信息
				member:[],//账户
				loading: false, //节流
				Sales_Message: '',
				pay: [{
					Name: '微信支付',
					Icon: '\ue650',
					isSelect: true,
					Color: '#28a745'
				}],
				isShopping_Cart:false,
				Activity_SerialNumber:'',//选中需要切换的活动
				showMask:false,
			}
		},
		onShow(){
			if (uni.getStorageSync("customerAddress")) {
				this.customerAddress = uni.getStorageSync("customerAddress");
			}
		},
		onLoad(options) {
			this.isShopping_Cart = options.isShopping_Cart=="1";
			this.Article = JSON.parse(decodeURIComponent(options.Article));
			console.log(this.Article);
			this.getData(this.Article[0].Activity_SerialNumber);
		},
		computed: {
			...mapState(['Point_Config','Open_ID','App_ID','WS_URL','BASE_IMG','Enterprise_Info_SerialNumber']),
			Data(){//将商品信息转换成需要的结构
				let arr = [];
				this.Article.forEach(item => {
					let obj = this.Points(item);
					let Amount = accMul(item.Quantity,item.Sales_Unit_Price);//商品总价
					if(arr.some(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)){//添加
						arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Children.push({
							...item,Reality_Amount:obj.Amount,Sales_Point:obj.Point,Amount,Deduction:obj.Deduction
						});
						let Sales_Point = arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Sales_Point;
						let Reality_Amount = arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Reality_Amount;
						let Sales_Amount = arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Sales_Amount;
						arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Sales_Point = accAdd(Sales_Point,obj.Point);
						arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Reality_Amount = accAdd(Reality_Amount,obj.Amount);
						arr.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber)[0].Sales_Amount = accAdd(Sales_Amount,Amount);
					} else {//新增
						let objMember = this.member.filter(i=>i.Activity_SerialNumber == item.Activity_SerialNumber);
						arr.push({
							Activity_SerialNumber:item.Activity_SerialNumber,
							Activity_Name:item.Activity_Name,
							Remain_Points:obj.Remain_Points,
							Sales_Point:obj.Point,
							Sales_Amount:Amount,
							Reality_Amount:obj.Amount,
							Member_Points_SerialNumber:objMember.Member_Points_SerialNumber?objMember.Member_Points_SerialNumber:'',
							Children:[{...item,Reality_Amount:obj.Amount,Sales_Point:obj.Point,Amount,Deduction:obj.Deduction}]
						})
					}
				})
				return arr;
			},
			Points(){//计算积分
				return function (obj){
					let Point = 0;//消耗积分
					let Amount = 0;//消耗金额
					let Deduction = 0;//抵扣金额
					try {
						//获取当前下所有商品活动积分总额
						let iPoints_Total = 0;//总共消耗积分
						let iobjPoint = 0;//当前商品消耗总积分
						//当前活动下剩余积分
						let member = this.member_zl[obj.Activity_SerialNumber].filter(i=>i.isSelect)[0];
						if(member.Deduction_Ratio != 0) {//如果没有活动，则该值为0
							this.Article.filter(i=>i.Activity_SerialNumber==obj.Activity_SerialNumber).forEach(item=>{
								let itemPoint = 0;//当前商品消耗积分
								//判断积分类型0固定上限，1订单百分比 获取当前商品积分
								if(item.Cash_Point=='0'){//是否可以使用积分
									if (item.Deduction_Type=='0') {//固定积分
										//若抵扣金额大于当前商品价格则按照商品价格的百分百   Deduction_Ratio:1积分等于多少分this.Point_Config => member
										if(parseFloat(item.Sales_Unit_Price) < parseFloat(accMul(item.Deduction_Limit,member.Deduction_Ratio)/100)){
											itemPoint = accDiv(accMul(item.Sales_Unit_Price,100),member.Deduction_Ratio);
										} else {
											itemPoint = item.Deduction_Limit;
										}
									} else if(item.Deduction_Type=='1'){//百分比积分
										//将积分转换成比例
										itemPoint = accDiv(accMul(item.Sales_Unit_Price,item.Deduction_Limit),member.Deduction_Ratio);
									}
									//累加 单商品积分 和 总共消耗积分
									iPoints_Total = accAdd(iPoints_Total,parseInt(accMul(itemPoint,item.Quantity)));
									//是否为当前商品
									if(obj.Supplier_Article_SerialNumber == item.Supplier_Article_SerialNumber) {
										iobjPoint = accMul(itemPoint,obj.Quantity);//当前商品消耗总积分
									}
								}
							})
						}
						//获取当前活动消耗积分获取积分占比
						let Points_Proportion = 0;
						if(iPoints_Total != 0){
							Points_Proportion = accDiv(parseInt(iobjPoint),iPoints_Total).toFixed(2);
						}
						//判断商品总积分是否大于当前活动剩余总积分
						let Remain_Points = member.Remain_Points;
						if(parseFloat(Remain_Points) < parseFloat(iPoints_Total)){
							//消耗积分
							Point = parseInt(accMul(Remain_Points,Points_Proportion));
							Deduction = accDiv(accMul(Point,member.Deduction_Ratio),100);//积分可抵扣的金额
							Amount = accSub(accMul(obj.Sales_Unit_Price,obj.Quantity),Deduction);//剩余金额
						} else {
							//消耗积分
							Point = parseInt(iobjPoint);
							Deduction = accDiv(accMul(Point,member.Deduction_Ratio),100);//积分可抵扣的金额
							Amount = accSub(accMul(obj.Sales_Unit_Price,obj.Quantity),Deduction);//剩余金额
						}
						return {Point,Deduction,Amount,Remain_Points};
					} catch(e) {
					 	//在获取到商品数据，账户数据之前，会报错
						return {Point,Deduction,Amount,Remain_Points:0};
					}
				}
			},
			member_zl(){//整理账户，形成指定json格式
				let obj1 = {};
				this.member.forEach(item=>{
					let obj = {
						Member_Points_SerialNumber:item.Member_Points_SerialNumber,
						Deduction_Ratio:item.Deduction_Ratio,
					    Remain_Points:item.Remain_Points,
					    Sales_Points:item.Sales_Points,
					    Total_Points:item.Total_Points,
						Department_SerialNumber:item.Department_SerialNumber,
						Department_Name:item.Department_Name,
						isSelect:item.isSelect,
					}
					let member = obj1[item.Activity_SerialNumber];
					if(member){
						member.push(obj);
					}else{
						obj1[item.Activity_SerialNumber] = [obj]
					}					
				})
				return obj1;
			},
			Total(){//统计价格数量
				let Quantity = 0;
				this.Article.forEach(item => {
					Quantity = Quantity + parseInt( item.Quantity);
				});
				let Reality_Amount = 0;
				let Sales_Amount = 0;
				this.Data.forEach(item => {
					Reality_Amount = accAdd(Reality_Amount, item.Reality_Amount)
					Sales_Amount = accAdd(Sales_Amount,item.Sales_Amount)
				})
				return {Quantity,Reality_Amount,Sales_Amount};
			},
			PointsColor(){
				return function (str){
					try {
						return this.member_zl[str].filter(i=>i.isSelect)[0].Department_SerialNumber?true:false;
					} catch(e) {
						return false;
					}
				}
			},
			Points_Prompt(){
				return function(str1,str2){
					if(str1=="0"){//固定
						return "不超过"+str2
					} else {//百分比
						return "不超过该商品总金额"+str2+"%"
					}
				}
			},
		},
		methods: {
			handleAddress() { //跳转选地址
				uni.navigateTo({
					url: "/pages/delivery/address?is=select",
				});
			},
			handleQuantity(val,item){//更新商品数量
				this.Article.filter(i=>i.Activity_SerialNumber==item.Activity_SerialNumber&&i.Supplier_Article_SerialNumber == item.Supplier_Article_SerialNumber)[0].Quantity = parseInt(val);
			},
			handlePay(item) { //选中支付方式
				this.pay.forEach(item => {
					item.isSelect = false;
				})
				item.isSelect = true;
			},
			handlePoints(SerialNumber,submit=false){//更换部门
				if(submit){//切换部门,关闭弹窗
					//关闭所有
					this.member.filter(i=>i.Activity_SerialNumber==this.Activity_SerialNumber).forEach(item=>{
						item.isSelect = false;
					})
					//更新选中
					this.member.filter(i=>i.Activity_SerialNumber==this.Activity_SerialNumber&&i.Department_SerialNumber == SerialNumber)[0].isSelect = true;
					this.showMask = false;
				}else{//开启弹窗，更换活动
					if(this.member_zl[SerialNumber].filter(i=>i.isSelect)[0].Department_SerialNumber){
						this.Activity_SerialNumber = SerialNumber;
						this.showMask = true;
					}
				}
			},
			async getData(){
				//获取商品所属活动，剩余积分数
				let arr = [];
				this.Article.forEach(item => {
					if(!arr.some(i=>i == item.Activity_SerialNumber)){//添加
						arr.push(item.Activity_SerialNumber)
					}
				})
				let sql="";
				arr.forEach((item,index)=>{
					if(index==0){
						sql = `business_member_cash_points.Activity_SerialNumber='${item}'`
					} else {
						sql = sql + ` OR business_member_cash_points.Activity_SerialNumber='${item}'`
					}
				})
				const res = await generalQueryBySQLSerialNumber({
					SQLSerialNumber:'M_business_member_cash_points',
					Condition:Base64.encode(this.Open_ID),
					ExtensionCondition:Base64.encode(`AND (${sql})`),
				})
				//校验，若本次商品有活动，但未查询到账户，则需要做处理，默认一个0积分账户
				if(arr.length > 0){//说明当前有活动，需要检验是否有对应账户，若没有，需要我们模拟一个为0积分的账户
					arr.forEach(item=>{
						//没有，默认一个账户
						if(!res.some(i=>i.Activity_SerialNumber==item)){
							res.push({
								Member_Points_SerialNumber:'',
								Activity_SerialNumber:item,
								Department_SerialNumber:'',
								Department_Name:'暂无可用积分',
								Remain_Points:0,
								Sales_Points:0,
								Total_Points:0,
								Deduction_Ratio:0,
							})
						}
					})
				}
				let activity = [];//默认第一个活动选中
				res.forEach(item=>{
					if(!activity.some(i=>i==item.Activity_SerialNumber)){
						item.isSelect = true;
						activity.push(item.Activity_SerialNumber);
					}else{
						item.isSelect = false;
					}
				})
				this.member = res;//空代表没有积分,
			},
			async submit() { //提交订单
				if (this.customerAddress != null) {
					if (!this.loading) {
						this.loading = true;
						uni.showLoading({
							title: "加载中...",
						});
						let business_sales_order_detail = [];
						let time = getCurrentTimestamp();
						if(!this.isShopping_Cart){//正常提交
							let item = this.Data[0].Children[0];
							//生效销售订单结构
							business_sales_order_detail.push(salesOrderDdetailJSON(item,this.Open_ID + '_' + time,1,this.Data[0].Member_Points_SerialNumber,this.member[0].Deduction_Ratio,this.Enterprise_Info_SerialNumber));
						} else {//购物车提交
							let index = 0;
							this.Data.forEach(obj=>{
								obj.Children.forEach(item=>{
									index++;
									business_sales_order_detail.push(salesOrderDdetailJSON(item,this.Open_ID + '_' + time,index,obj.Member_Points_SerialNumber,this.member[0].Deduction_Ratio,this.Enterprise_Info_SerialNumber))
								})
							})
						}
						let business_sales_order = [{
							Sales_Order_SerialNumber:this.Open_ID + '_' + time,
							Store_SerialNumber:business_sales_order_detail[0].Store_SerialNumber,
							Sales_Address:`${this.customerAddress.Province}/${this.customerAddress.City}/${this.customerAddress.County}/${this.customerAddress.Details}`,
							Sales_Contacts:this.customerAddress.Name,
							Sales_Phone:this.customerAddress.Phone,
							Sales_Message:this.Sales_Message,
							Sales_Date:DateTime(),
							Collection_Date:'',
							Open_ID:this.Open_ID,
							App_ID:this.App_ID,
							Sales_Amount:this.Total.Sales_Amount,
							Collection_Amount:0,
							Order_Type:'0',
							Refund_Type:'',
							Activity_SerialNumber:business_sales_order_detail[0].Activity_SerialNumber,
							Row_Status:this.isShopping_Cart?'11':'01',
							Children:{
								business_sales_order_detail
							}
						}]
						let sales_order = {};
						this.Article.forEach(item=>{
							item.isSelect = undefined
						})
						sales_order[this.Open_ID] = {Add:{business_sales_order}};
						if(this.isShopping_Cart){
							sales_order[this.Open_ID].Modify = { business_shopping_trolley:this.Article }
						} 
						this.WebSocket(sales_order);
					}
				}else {
					uni.showToast({
						title: '您还未选择地址！',
						icon: 'error'
					})
				}
			},
			collectionJSON(business_sales_order,business_sales_order_detail){
				let business_collection_detail = [];
				// let objSalesOrder = business_sales_order[this.Open_ID].Add;
				// objSalesOrder.business_sales_order[0].Children.business_sales_order_detail.forEach(item=>{
				// 	if(parseFloat(item.Reality_Amount) > 0){
				// 		business_collection_detail.push(collectionDetailJSON(item,'微信支付',item.Reality_Amount,''));
				// 	}
				// 	//积分，目前只有现金积分
				// 	if(parseFloat(item.Sales_Point) > 0){
				// 		let obj = this.member_zl[item.Activity_SerialNumber].filter(i=>i.isSelect)[0];
				// 		business_collection_detail.push(collectionDetailJSON(item,'现金积分消费',item.Sales_Point,obj.Member_Points_SerialNumber));
				// 	}
				// })
				business_sales_order_detail.forEach(item=>{
					if(parseFloat(item.Reality_Amount) > 0){
						business_collection_detail.push(collectionDetailJSON(item,'微信支付',item.Reality_Amount,''));
					}
					//积分，目前只有现金积分
					if(parseFloat(item.Sales_Point) > 0){
						let obj = this.member_zl[item.Activity_SerialNumber].filter(i=>i.isSelect)[0];
						business_collection_detail.push(collectionDetailJSON(item,'现金积分消费',item.Sales_Point,obj.Member_Points_SerialNumber));
					}
				})
				// 形成收款表
				let business_collection = [{
					Collection_SerialNumber:'',
					Store_SerialNumber:business_collection_detail[0].Store_SerialNumber,
					Order_Type:'1',
					Sales_Date:business_sales_order[0].Sales_Date,
					Collection_Date:'',
					Sales_Amount:business_sales_order[0].Sales_Amount,
					Collection_Amount:'0',
					Uncollected_Amount:business_sales_order[0].Sales_Amount,
					Change_Amount:'0',
					Open_ID:this.Open_ID,
					App_ID:this.App_ID,
					Children:{
						business_collection_detail
					}
				}]
				return {business_collection};
			},
			async generalSave(Data,callback){//同意修改
				callback(await generalSave(Data));
			},
			async saveSalesOrder(Data,callback){//提交订单
				callback(await saveSalesOrder(Data));
			},
			async generalQueryByTableName(Data,callback){//通用查询表名
				callback(await generalQueryByTableName(Data));
			},
			async saveReceipts(Data,callback){//调用支付
				callback(await saveReceipts(Data));
			},
			async rewriteReceipts(Data,callback){
				callback(await rewriteReceipts(Data));
			},
			async getMemberPoint(Data,callback){
				callback(await getMemberPoint(Data))
			},
			WebSocket(sales_order) {
				const that = this;
				this.websocket = new WebSocketIO(`/salesOrder/${this.Open_ID}`, this.WS_URL, 10000)
				this.websocket.connectSocketInit();
				uni.$on("connectOpen",(res)=>{//连接成功
					this.websocket.send(sales_order);
				});
				uni.$on("socketMsg", (res) => {
					res = JSON.parse(res)
					this.websocket.closeSocket();
					uni.hideLoading();
					if(res.Status=='success'){
						//查询销售订单
						uni.showLoading({
							title: "调起支付...",
						});
						this.getMemberPoint({
							Data: JSON.stringify([{
							    tableName: 'business_sales_order',
							    Condition: Base64.encode(unescape(encodeURIComponent("(Row_Status = '01' OR Row_Status = '11') AND Sales_Order_SerialNumber='"+res.Data+"'"))),
							}, {
                                tableName: 'business_sales_order_detail',
							    Condition: Base64.encode(unescape(encodeURIComponent("Sales_Order_SerialNumber='"+res.Data+"'"))),
							}]),
						},({business_sales_order,business_sales_order_detail})=>{
							if(business_sales_order.length==1&&business_sales_order_detail.length>=1){
								if(business_sales_order[0].Row_Status == '01'){
									business_sales_order[0].Row_Status = '02'
								} else if (business_sales_order[0].Row_Status == '11'){
									business_sales_order[0].Row_Status = '12'
								}
								let business_collection = this.collectionJSON(business_sales_order,business_sales_order_detail);
								let obj = {
									Add: business_collection,
									Modify: {business_sales_order}
								}
								//提交支付申请
								this.saveReceipts({Data:JSON.stringify(obj)},(res)=>{
									uni.hideLoading();
									if(res.Status == 'success'){
										let Colletion_SerialNumber = res.Data;
										uni.showModal({
											title: "确认",
											content: "是否使用积分进行支付",
											success: (res)=> {
												if (res.confirm) {
													
													uni.showLoading({
														title: "支付中...",
													});
													this.rewriteReceipts({Colletion_SerialNumber},(res)=>{
														uni.showToast({
															title: '支付成功！',
															icon: 'none'
														})
														uni.redirectTo({
															url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
														})
														uni.hideLoading();
													});
												} else if (res.cancel) {
													uni.showToast({
														title:'已取消支付！',
														icon:'none'
													})
													
												}
											},
										})
									}else if (res.prepay_id){ //调用微信支付
										let Colletion_SerialNumber = res.Data;
										//微信支付
										weChatPay(res,()=>{//成功
											uni.showToast({
												title: '支付成功!',
												icon: 'success',
											})
											//复写订单状态为8待修改
											that.generalQueryByTableName({
												TableName:'business_collection',
												Condition: "Row_Status = '0' AND Collection_SerialNumber='"+Colletion_SerialNumber+"'"
											},(data)=>{
												if(data.length==1){
													data[0].Row_Status ='8';
													that.generalSave({
														Data:JSON.stringify({Modify:{business_collection:data}})
													},()=>{
														uni.redirectTo({
															url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
														})
													})
												}else{
													uni.redirectTo({
														url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
													})
												}
											});
										},()=>{//失败
											uni.showToast({
												title: '已取消支付！',
												icon: 'error'
											})
											uni.redirectTo({
												url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
											})
										},()=>{//成功失败都会调
											this.loading = false
											uni.hideLoading();
										})
									}
									this.loading = false;
								});
							}
						})
						
						// this.generalQueryByTableName({
						// 	TableName:'business_sales_order',
						// 	Condition: "(Row_Status = '01' OR Row_Status = '11') AND Sales_Order_SerialNumber='"+res.Data+"'"
						// },(business_sales_order)=>{
						// 	if(business_sales_order.length==1){
						// 		if(business_sales_order[0].Row_Status == '01'){
						// 			business_sales_order[0].Row_Status = '02'
						// 		} else if (business_sales_order[0].Row_Status == '11'){
						// 			business_sales_order[0].Row_Status = '12'
						// 		}
						// 		let business_collection = this.collectionJSON(sales_order,res.Data);
						// 		let obj = {
						// 			Add: business_collection,
						// 			Modify: {business_sales_order}
						// 		}
						// 		//提交支付申请
						// 		this.saveReceipts({Data:JSON.stringify(obj)},(res)=>{
						// 			uni.hideLoading();
						// 			if(res.Status == 'success'){
						// 				let Colletion_SerialNumber = res.Data;
						// 				uni.showModal({
						// 					title: "确认",
						// 					content: "是否使用积分进行支付",
						// 					success: (res)=> {
						// 						if (res.confirm) {
													
						// 							uni.showLoading({
						// 								title: "支付中...",
						// 							});
						// 							this.rewriteReceipts({Colletion_SerialNumber},(res)=>{
						// 								uni.showToast({
						// 									title: '支付成功！',
						// 									icon: 'none'
						// 								})
						// 								uni.redirectTo({
						// 									url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
						// 								})
						// 								uni.hideLoading();
						// 							});
						// 						} else if (res.cancel) {
						// 							uni.showToast({
						// 								title:'已取消支付！',
						// 								icon:'none'
						// 							})
						// 						}
						// 					},
						// 				})
						// 			}else if (res.prepay_id){ //调用微信支付
						// 				let Colletion_SerialNumber = res.Data;
						// 				//微信支付
						// 				weChatPay(res,()=>{//成功
						// 					uni.showToast({
						// 						title: '支付成功!',
						// 						icon: 'success',
						// 					})
						// 					//复写订单状态为8待修改
						// 					that.generalQueryByTableName({
						// 						TableName:'business_collection',
						// 						Condition: "Row_Status = '0' AND Collection_SerialNumber='"+Colletion_SerialNumber+"'"
						// 					},(data)=>{
						// 						if(data.length==1){
						// 							data[0].Row_Status ='8';
						// 							that.generalSave({
						// 								Data:JSON.stringify({Modify:{business_collection:data}})
						// 							},()=>{
						// 								uni.redirectTo({
						// 									url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
						// 								})
						// 							})
						// 						}else{
						// 							uni.redirectTo({
						// 								url:'/pages/my/myOrderDetail?salesOrder='+business_sales_order[0].Sales_Order_SerialNumber
						// 							})
						// 						}
						// 					});
						// 				},()=>{//失败
						// 					uni.showToast({
						// 						title: '已取消支付！',
						// 						icon: 'error'
						// 					})
						// 				},()=>{//成功失败都会调
						// 					this.loading = false
						// 					uni.hideLoading();
						// 				})
						// 			}
						// 			this.loading = false;
						// 		});
						// 	}else{
						// 		this.loading = false;
						// 		uni.hideLoading();
						// 		uni.showToast({
						// 			title: '系统繁忙！',
						// 			icon: 'none'
						// 		})
						// 	}
						// })
					} else if (res.Status == "Fail") {
						// uni.hideLoading();
						this.loading = false;
						// if(!this.isStock.some(i=>i==res.Description)){
						// 	this.isStock=[...this.isStock,res.Description]
						// }
						//console.log(this.isStock);
						uni.showToast({
							title: '库存不足！',
							icon: 'error'
						})
					}
				})
			},
			
		}
	}
</script>
<style>

</style>